package TestRecursion;

import java.util.Scanner;

public class TestRecursion {
    public static void main(String[] args) {
        //在高性能的要求下,尽量避免使用递归,递归耗时耗内存
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        long t1=System.currentTimeMillis();//返回当前的时刻:毫秒数
        System.out.println(n+"的阶乘结果是"+factorial(n));
        long t2=System.currentTimeMillis();
        long time=t2-t1;
        System.out.println("递归费时:"+time);
        factorialLoop(n);
    }
    static long factorial(int n){
        if(n==1){
            return 1;
        }else {
            return n*factorial(n-1);
        }
    }
    static long factorialLoop(int n){
        long t3=System.currentTimeMillis();
        long result=1;
        for (int i=1;i<=n;i++){
            result=result*i;
        }
        long t4=System.currentTimeMillis();
        long time2=t4-t3;
        System.out.println(n+"的阶乘是:"+result);
        System.out.println("普通循环费时:"+time2);
        return result;
    }
}
